package com.eblacorp.archive.view.bean.realtydocument;

import java.io.File;
import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.apache.commons.lang.SerializationUtils;
import org.icefaces.ace.component.fileentry.FileEntry;
import org.icefaces.ace.component.fileentry.FileEntryEvent;
import org.icefaces.ace.component.fileentry.FileEntryResults;

import com.eblacorp.archive.domain.fn.RealtyDocument;
import com.eblacorp.archive.service.RealtyDocumentService;
import com.eblacorp.archive.view.utils.FacesUtils;

@ManagedBean
@SessionScoped
public class RealtyDocumentBean implements Serializable {

	private static final long serialVersionUID = -8486486280143002453L;
	private RealtyDocument realtyDocument;
	private RealtyDocument searchDocument;
	@ManagedProperty(value = "#{realtyDocumentService}")
	private RealtyDocumentService realtyDocumentService;
	private File file;
	private List<RealtyDocument> searchResult;
	private String url;
	private Integer editIndex;
	
	public RealtyDocumentBean() {
		realtyDocument = new RealtyDocument();
		searchDocument=new RealtyDocument();
	}

	public void save() {
		try {
			RealtyDocument newDocument = null;
			if(editIndex!=null){
				newDocument = realtyDocumentService.updateDocument(realtyDocument, file);
				searchResult.set(editIndex.intValue(), newDocument);
			}else{
				newDocument = realtyDocumentService.addNewDocument(realtyDocument, file);
			}
			
			if (newDocument != null) {
				FacesUtils.addMsg("msg.lable.saved",FacesMessage.SEVERITY_INFO, "");
			} else {
				FacesUtils.addMsg("msg.lable.faild",FacesMessage.SEVERITY_ERROR, "");
			}
		} catch (Exception e) {
			FacesUtils.addMsg("msg.lable.exception",FacesMessage.SEVERITY_ERROR, "");
			e.printStackTrace();
		} finally {
			realtyDocument = new RealtyDocument();
			editIndex=null;
			file=null;
		}
	}
	public void edit(Integer index){
		editIndex=index;
		realtyDocument=	(RealtyDocument) SerializationUtils.clone(searchResult.get(index.intValue()));
		
	}

	public void fileListener(FileEntryEvent e) {
		FileEntry fe = (FileEntry) e.getComponent();
		FileEntryResults results = fe.getResults();
		File parent = null;

		// get data About File
		for (FileEntryResults.FileInfo i : results.getFiles()) {

			if (i.isSaved()) {

				file = i.getFile();

			} else {
			}
		}
	}

	public void search() {
		try {
			searchResult = realtyDocumentService.search(searchDocument, null,
					null, false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void getViewerUrl(Integer Index){
		url= realtyDocumentService.getViewerUrl(searchResult.get(Index.intValue()));
	}
	
	public void delete(Integer Index){
		
		try {
			RealtyDocument document=searchResult.get(Index.intValue());
			realtyDocumentService.deleteDocument(document, true);
			searchResult.remove(Index.intValue());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public RealtyDocument getRealtyDocument() {
		return realtyDocument;
	}

	public void setRealtyDocument(RealtyDocument realtyDocument) {
		this.realtyDocument = realtyDocument;
	}

	public RealtyDocumentService getRealtyDocumentService() {
		return realtyDocumentService;
	}

	public void setRealtyDocumentService(
			RealtyDocumentService realtyDocumentService) {
		this.realtyDocumentService = realtyDocumentService;
	}
	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public List<RealtyDocument> getSearchResult() {
		return searchResult;
	}

	public void setSearchResult(List<RealtyDocument> searchResult) {
		this.searchResult = searchResult;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Integer getEditIndex() {
		return editIndex;
	}

	public void setEditIndex(Integer editIndex) {
		this.editIndex = editIndex;
	}

	public RealtyDocument getSearchDocument() {
		return searchDocument;
	}

	public void setSearchDocument(RealtyDocument searchDocument) {
		this.searchDocument = searchDocument;
	}

}
